﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.ClickCricket.bll;

/// <summary>
/// Summary description for LoginLinqProvider
/// </summary>
/// 
namespace com.ClickCricket.dal
{
    public class LoginLinqProvider : ILoginProvider
    {
        public LoginLinqProvider()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Login getUser(int userID)
        {
            using (var dc = new Context.CricketAdminmDataContext())
            {
                var login = from aLogin in dc.Logins
                            where aLogin.LoginID == userID
                            select aLogin;

                if (login == null)
                {
                    return null;
                }
                if (login.Count<Entity.Login>() == 1)
                {
                    return extractLogin(login);
                }
                throw new Exception("Invalid Login Id " + userID + " !");
            }
        }

        private Login extractLogin(IQueryable<Entity.Login> login)
        {
            Login aLogin = new Login(
                login.Single<Entity.Login>().LoginID,
                login.Single<Entity.Login>().UserName,
                login.Single<Entity.Login>().Password);
            return aLogin;
        }

    }
}